package com.kyzh.core.pager.weal.live.adapter

import android.graphics.Color
import android.text.SpannableString
import android.text.Spanned
import android.text.style.ForegroundColorSpan
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.kyzh.core.R
import com.kyzh.core.pager.weal.live.msgbean.MsgEntity

class LiveChatAdapter :BaseQuickAdapter<MsgEntity, BaseViewHolder>(R.layout.item_groupchat_msg) {
    override fun convert(holder: BaseViewHolder, item: MsgEntity) {
        var content = ""
        if(!item.content.isNullOrEmpty()){
            if(item.content!!.startsWith("playwith#giftName#送")) {
                kotlin.runCatching {
                    val spGift = item.content!!.split("giftName#")
                    content = spGift[1]
                }.onFailure {
                    content =item.content!!
                }
            }else{
                content =item.content!!
            }
        }else{
            content =item.content!!
        }
        if(item.type == MsgEntity.TYPE_JOIN){

            holder.setBackgroundColor(R.id.tvMsg,Color.parseColor("#FCF7FF"))
        }else{
            holder.setBackgroundColor(R.id.tvMsg,Color.parseColor("#FFFEFF"))
        }
        val s = item.userName + ":" + content
        val spannableString = SpannableString(s)
        spannableString.setSpan(ForegroundColorSpan(Color.parseColor("#FF8D9AB1")),0,
            item.userName?.length!! +1,
            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        holder.setText(R.id.tvMsg,spannableString)
    }
}